package org.arnetminer.personalized.service;

import java.util.ArrayList;
import java.util.List;

import org.arnetminer.personalized.dao.EventDAO;
import org.arnetminer.personalized.util.Global;
import org.arnetminer.personalized.vo.Event;
import org.arnetminer.personalized.vo.EventType;
import org.arnetminer.personalized.vo.Follow;
import org.arnetminer.personalized.vo.FollowType;
import org.arnetminer.personalized.vo.Notification;
import org.arnetminer.personalized.vo.User;

public class EventServiceImpl implements EventService {

	private EventDAO eventDAO;

	public EventDAO getEventDAO() {
		return eventDAO;
	}

	public void setEventDAO(EventDAO eventDAO) {
		this.eventDAO = eventDAO;
	}

	private FollowService followService;

	public FollowService getFollowService() {
		return followService;
	}

	public void setFollowService(FollowService followService) {
		this.followService = followService;
	}

	@Override
	public boolean save(Event event) {
		try {
			eventDAO.save(event);
			
			// send notification to followers
			List<String> properties = new ArrayList();
			List values = new ArrayList();

			properties.add("followee");
			values.add(event.getSubId());

			properties.add("type");
			FollowType type = followService.getFollowType(Global.LOCALTYPE);
			values.add(type);
			List<Follow> follows = followService.getByProperties(properties,
					values);

			for (Follow follow : follows) {
				Notification notification = new Notification();
				notification.setEvent(event);
				notification.setNotifiee(follow.getFollower());
				this.notify(notification);
			}

		} catch (Exception ex) {
			ex.printStackTrace();
			return false;
		}
		return true;
	}

	@Override
	public EventType getEventType(long id) {
		EventType type = eventDAO.findEventTypeById(id);
		return type;
	}
	
	

	@Override
	public List<Event> getEventsByProperties(List<String> properties,
			List values, int start, int max) {
		return eventDAO.findEventsByProperties(properties, values, start, max);
	}

	@Override
	public boolean notify(Notification notification) {
		try {
			eventDAO.saveNotification(notification);
		} catch (Exception ex) {
			ex.printStackTrace();
			return false;
		}
		return true;
	}

	@Override
	public List getUserNotification(User user, int start, int max) {
		
		List<String> properties = new ArrayList();
		List values = new ArrayList();
		properties.add("notifiee");
		values.add(user);
		List notis = eventDAO.findNotificationsByProperties(properties, values,
				start, max);
		return notis;
	}
	
	

	@Override
	public List<Notification> getNotificationsByProperties(
			List<String> properties, List values, int start, int max) {
		
		return eventDAO.findNotificationsByProperties(properties, values, start, max);
	}

	@Override
	public boolean deleteNotification(long id, User user) {

		List<String> properties = new ArrayList();
		List values = new ArrayList();
		properties.add("notifiee");
		values.add(user);

		properties.add("id");
		values.add(id);

		List<Notification> notis = eventDAO.findNotificationsByProperties(
				properties, values, 0, 1);

		if (notis == null || notis.size() == 0) {
			return false;
		}

		try {
			eventDAO.deleteNotification(notis.get(0));
		} catch (Exception ex) {
			ex.printStackTrace();
			return false;
		}
		return true;
	}

	@Override
	public boolean deleteEventByEventId(long id) {
		
		List<String> properties = new ArrayList<String>();
		List values = new ArrayList();
		properties.add("eventId");
		values.add(id);
		List<Event> events = eventDAO.findEventsByProperties(properties,
				values, 0, 1);
		if (events == null || events.size() == 0) {
			System.out.println("null");
			return false;
		}

		try {
			eventDAO.delete(events.get(0));
		} catch (Exception ex) {
			ex.printStackTrace();
			return false;
		}
		return true;
	}

}
